package com.buybuyyi.stylistcrm.resource.goods.service;

import java.io.InputStream;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buybuyyi.stylistcrm.resource.goods.dao.GoodspicturesDao;
import com.buybuyyi.stylistcrm.resource.goods.po.Goodspictures;

import stylistcrm.core.services.goods.dto.FileUploadDTO;
import stylistcrm.core.services.goods.service.IFileUploadService;
import stylistcrm.core.services.resource.ObjectResourceService;

@Service
public class FileUploadService implements IFileUploadService{

	
	@Autowired
	private ObjectResourceService objectResourceService;
	
	@Autowired
	private GoodspicturesDao goodspicturesDao;

	@Override
	public FileUploadDTO fileUpload(InputStream stream,String fileName,Long uerId,String type) {
		String uuid=UUID.randomUUID().toString();
		FileUploadDTO uploadDto=new FileUploadDTO();
		try {
			//上传文件
			String fileUrl=objectResourceService.preSave(stream, fileName, null, uuid, "", uerId);
			//记录数据库
			Goodspictures goodpicture=new Goodspictures();
			//TODO
			goodpicture.setCategory(0);
			goodpicture.setImageUrl(fileUrl);
			Long pictureId=goodspicturesDao.insert(goodpicture);
			
			uploadDto.setFileName(fileName);
			uploadDto.setUrl(fileUrl);
			uploadDto.setIsSuccess("1");
			uploadDto.setFileId(pictureId);
		} catch (Exception e) {
			uploadDto.setIsSuccess("0");
			uploadDto.setMessage(e.getMessage());
		}
		return uploadDto;
	}

}
